﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using timetableasy.Models;
using System.Web.Security;

namespace timetableasy.Controllers
{
    public class UsersController : AccountController
    {
        static UserRepository userRepository = new UserRepository();
        //
        // GET: /Useres/

        public ActionResult Index()
        {
            var users = userRepository.FindAllUsers().ToList();
            return View("Index", users);
        }

        //
        // GET: /Useres/Details/5

        public ActionResult Details(int id)
        {
            User user = userRepository.GetUserById(id);
            if (user == null)
                return View("NotFound");
            else
                return View("Details", user);
        }

        //
        // GET: /Useres/Create

        public ActionResult Create()
        {
            User user = new User()
            {
                LastName = string.Empty,
                FirstName = string.Empty,
                aspnet_Role = new aspnet_Role(),
                Login = string.Empty,
                Password = string.Empty
            };

            return View(user);
        }

        //
        // POST: /Useres/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                ModelStateDictionary m = CreateUser(user, ModelState);
                if(m.IsValid)
                    return RedirectToAction("Details", new { id = user.UserId });
            }

            return View(user);
        }

        //
        // GET: /Useres/Edit/5

        public ActionResult Edit(int id)
        {
            User user = userRepository.GetUserById(id);
            if (user != null)
            {
                return View(user);
            }
            else
            {
                return RedirectToAction("Index");
            }

        }

        //
        // POST: /Useres/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            User user = userRepository.GetUserById(id);
            if (user == null)
                return View("NotFound");
            try
            {
                UpdateModel(user);
                userRepository.Save();
                return RedirectToAction("Details", new { id = user.UserId });
            }
            catch
            {
                foreach (var issue in user.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
                return View(user);
            }
        }

        //
        // GET: /Useres/Delete/5

        public ActionResult Delete(int id)
        {
            User user = userRepository.GetUserById(id);

            if (user == null)
                return View("NotFound");

            //if (!User.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            return View(user);
        }

        // 
        // HTTP POST: /Dinners/Delete/1

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            User user = userRepository.GetUserById(id);

            if (user == null)
                return View("NotFound");

            //if (!User.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            userRepository.Delete(user);
            userRepository.Save();

            return View("Deleted");
        }


        public static void CreateEditProfile(User model)
        {
            MembershipUser user = Membership.GetUser(model.Login);
            ProfileCommon profile = ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;

            profile.FirstName = model.FirstName;
            profile.LastName = model.LastName;
            profile.Save();
        }

        public virtual ModelStateDictionary CreateUser(User u, ModelStateDictionary m)
        {
            try
            {
                userRepository.Add(u);
                userRepository.Save();
            }
            catch
            {
                foreach (var issue in u.GetRuleViolations())
                {
                    m.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
            }
            return m;            
        }
    }
}
